import diff from "./diff"

/**
 * 更新组件内容
 * @param {*} virtualDOM 
 * @param {*} oldComponent 
 * @param {*} oldDOM 
 * @param {*} container 
 */
export default function updateComponent(virtualDOM, oldComponent, oldDOM, container) {
  oldComponent.componentWillReceiveProps(virtualDOM.props)
  // 通过父类的声明周期函数，判断组件内容是否需要更新
  if (oldComponent.shouldComponentUpdate(virtualDOM.props)) {
    // 获取未更新前的 props
    let prevProps = oldComponent.props
    // 组件更新通知的生命周期函数
    oldComponent.componentWillUpdate(virtualDOM.props)
    oldComponent.updateProps(virtualDOM.props)
    let nextVirtualDOM = oldComponent.render()
    nextVirtualDOM.component = oldComponent
    diff(nextVirtualDOM, container, oldDOM)
    oldComponent.componentDidUpdate(prevProps)
  }

}